package cn.iocoder.yudao.module.system.service.erp.operatelog;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.operatelog.vo.ErpOperateLogPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.operatelog.vo.ErpOperateLogSaveReqVO;
import cn.iocoder.yudao.module.system.core.ErpOperationLog;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.operatelog.ErpOperateLogDO;
import cn.iocoder.yudao.module.system.dal.mysql.erp.operatelog.ErpOperateLogMapper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.reflect.Method;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.OPERATE_LOG_NOT_EXISTS;
import static java.util.Objects.requireNonNull;
import static org.springframework.web.context.request.RequestContextHolder.getRequestAttributes;

/**
 * erp操作日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ErpOperateLogServiceImpl implements ErpOperateLogService {

    @Resource
    private ErpOperateLogMapper erpOperateLogMapper;

    @Override
    public Long createOperateLog(ErpOperateLogSaveReqVO createReqVO) {
        // 插入
        ErpOperateLogDO operateLog = BeanUtils.toBean(createReqVO, ErpOperateLogDO.class);
        erpOperateLogMapper.insert(operateLog);
        // 返回
        return operateLog.getId();
    }

    @Override
    public void updateOperateLog(ErpOperateLogSaveReqVO updateReqVO) {
        // 校验存在
        validateOperateLogExists(updateReqVO.getId());
        // 更新
        ErpOperateLogDO updateObj = BeanUtils.toBean(updateReqVO, ErpOperateLogDO.class);
        erpOperateLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteOperateLog(Long id) {
        // 校验存在
        validateOperateLogExists(id);
        // 删除
        erpOperateLogMapper.deleteById(id);
    }

    private void validateOperateLogExists(Long id) {
        if (erpOperateLogMapper.selectById(id) == null) {
            throw exception(OPERATE_LOG_NOT_EXISTS);
        }
    }

    @Override
    public ErpOperateLogDO getOperateLog(Long id) {
        return erpOperateLogMapper.selectById(id);
    }

    @Override
    public PageResult<ErpOperateLogDO> getOperateLogPage(ErpOperateLogPageReqVO pageReqVO) {
        return erpOperateLogMapper.selectPage(pageReqVO);
    }

    @Override
    public Long createOperateLog(Long userId,Method method) {
        return createOperateLog(userId,"",method);
    }

    @Override
    public Long createOperateLog(Long userId,String data, Method method) {
        if(ObjectUtil.isNull(userId)){
            return 0L;
        }
        ErpOperateLogDO operateLog = new ErpOperateLogDO();
        ErpOperationLog erpOperationLog = method.getAnnotation(ErpOperationLog.class);
        operateLog.setUserId(userId);
        operateLog.setModule(erpOperationLog.module());
        String description = ObjectUtil.isNull(erpOperationLog.description())?"":erpOperationLog.description();
        if(StrUtil.isNotBlank(data)){
            description=description.concat(data);
        }
        operateLog.setDescription(description);
        operateLog.setOperateIp(HttpUtils.getClientPublicIP(((ServletRequestAttributes) requireNonNull(getRequestAttributes())).getRequest()));
        erpOperateLogMapper.insert(operateLog);
        return operateLog.getId();
    }
}